# -*- coding: utf-8 -*-
import re

from pyquery import PyQuery

from base.gsxt_base_worker import GsxtBaseWorker
from common.global_field import Model

'''
1. 搜索没有结果判断功能添加
'''


class GsxtXinJiangWorker(GsxtBaseWorker):
    def __init__(self, **kwargs):
        GsxtBaseWorker.__init__(self, **kwargs)

    def get_search_result_html(self, keyword, session):
        param_list = []
        try:
            url = 'http://{host}/sydq/loginSydqAction!sydq.dhtml'.format(host=self.host)
            content = self.get_captcha_geetest(url, '#keyword_qycx', '#popup-submit', keyword, '.search-result',
                                               proxy_type=2)
            if content is None:
                return param_list, self.SEARCH_ERROR

            jq = PyQuery(content, parser='html')
            if jq.find('div.search-result').find('p.total').find('span.light').text() == '0':
                return param_list, self.SEARCH_NOTHING_FIND

            pattern = '&quot;/gjjbj/gjjQueryCreditAction!queryEntIndexInfo\.dhtml\?entId=(.*?)&amp;clear=true&amp;urltag=1&amp;urlflag=0&amp;credit_ticket=(.*?)&quot'
            regex = re.compile(pattern)
            search_list = regex.findall(content)
            param_set = set()
            if len(search_list) > 0:
                for item in search_list:
                    hash_obj = item[0] + '_' + item[1]
                    if hash_obj in param_set:
                        continue

                    param_list.append({'credit_ticket': item[1], 'entId': item[0]})
                    param_set.add(hash_obj)
        except Exception as e:
            self.log.exception(e)
            return param_list, self.SEARCH_ERROR

        return param_list, self.SEARCH_SUCCESS if len(param_list) > 0 else self.SEARCH_ERROR

    @staticmethod
    def get_company_name(text):
        pattern = '<td><strong>企业名称：</strong>(.*?)</td>'
        search_list = re.findall(pattern, text.encode('utf-8'))
        if len(search_list) > 0:
            return search_list[0].strip()

        pattern = '<td><strong>名称：</strong>(.*?)</td>'
        search_list = re.findall(pattern, text.encode('utf-8'))
        if len(search_list) > 0:
            return search_list[0].strip()

        return None

    def get_detail_info(self, session, href, info, data):
        if href is None or href == '':
            return

        url = 'http://{host}{href}'.format(host=self.host, href=href)
        r = self.task_request(session.get, url=url)
        if r is None:
            return

        self.append_model(data, info, url, r.text)

    def get_nb_detail_info(self, session, text, data, year):
        jq = PyQuery(text, parser='html')

        # 网站网店
        href = jq.find('#wzFrame').attr('src')
        if href is not None and href != '':
            url = 'http://{host}{href}'.format(host=self.host, href=href)
            r = self.task_request(session.get, url)
            if r is not None:
                self.append_model(data, Model.annual_info, url, r.text, year=year, classify=Model.type_detail)

        # 股东信息
        href = jq.find('#gdczFrame').attr('src')
        if href is not None and href != '':
            url = 'http://{host}{href}'.format(host=self.host, href=href)
            r = self.task_request(session.get, url)
            if r is not None:
                self.append_model(data, Model.annual_info, url, r.text, year=year, classify=Model.type_detail)

        # 对外投资
        href = jq.find('#dwtzFrame').attr('src')
        if href is not None and href != '':
            url = 'http://{host}{href}'.format(host=self.host, href=href)
            r = self.task_request(session.get, url)
            if r is not None:
                self.append_model(data, Model.annual_info, url, r.text, year=year, classify=Model.type_detail)

        # 对外担保
        href = jq.find('#dwdbFrame').attr('src')
        if href is not None and href != '':
            url = 'http://{host}{href}'.format(host=self.host, href=href)
            r = self.task_request(session.get, url)
            if r is not None:
                self.append_model(data, Model.annual_info, url, r.text, year=year, classify=Model.type_detail)

        # 变更信息
        href = jq.find('#xgFrame').attr('src')
        if href is not None and href != '':
            url = 'http://{host}{href}'.format(host=self.host, href=href)
            r = self.task_request(session.get, url)
            if r is not None:
                self.append_model(data, Model.annual_info, url, r.text, year=year, classify=Model.type_detail)

    def get_year_info_list(self, text):
        jq = PyQuery(text, parser='html')
        tr_list = jq.find('tr').items()
        for index, item in enumerate(tr_list):
            if index == 0:
                continue
            try:
                year_info = item.find('td').eq(1).text()
                href = item.find('a').attr('href')

                year_list = re.findall('(\d+)', year_info)
                year = str(year_list[0]) if len(year_list) > 0 else None
                if href is None or href == '':
                    continue
                if year is None or year == '':
                    continue
                yield year, 'http://{host}{href}'.format(host=self.host, href=href)
            except Exception as e:
                self.log.exception(e)

    # 获得变更信息
    def get_change_info(self, session, data, credit_ticket, ent_id):
        url = 'http://{host}/gjjbj/gjjQueryCreditAction!xj_biangengFrame.dhtml?clear=true&credit_ticket={credit_ticket}'.format(
            host=self.host, credit_ticket=credit_ticket
        )
        post_data = {
            'pageNos': '1',
            'ent_id': ent_id,
            'urltag': '5',
            'pageSize': '5',
            'pageNo': '1',
        }

        r = self.task_request(session.post, url, data=post_data)
        if r is None:
            return None

        if r.text.find('暂无变更信息') != -1:
            return None

        self.append_model(data, Model.change_info, url, r.text, post_data=post_data)

        pattern = '共查询到&nbsp;.*?&nbsp;条信息&nbsp;&nbsp;共(.*?)页&nbsp;&nbsp;'
        search_list = re.findall(pattern, r.text.encode('utf-8'))
        if len(search_list) > 0:
            page_num = int(search_list[0])
            if page_num >= 50:
                page_num = 50
            for page in xrange(2, page_num + 1):
                post_data = {
                    'pageNos': page,
                    'ent_id': ent_id,
                    'urltag': '5',
                    'pageSize': '5',
                    'pageNo': page - 1,
                }
                r = self.task_request(session.post, url, data=post_data)
                if r is None:
                    continue

                if r.text.find('暂无变更信息') != -1:
                    continue

                self.append_model(data, Model.change_info, url, r.text, post_data=post_data)
        return data

    # 获得主要人员信息
    def get_key_person_info(self, session, data, credit_ticket, ent_id):
        url = 'http://{host}/gjjbj/gjjQueryCreditAction!zyryFrame.dhtml?flag=more&ent_id={entid}&credit_ticket={credit_ticket}'.format(
            host=self.host, entid=ent_id, credit_ticket=credit_ticket)
        r = self.task_request(session.get, url)
        if r is None:
            return None
        self.append_model(data, Model.key_person_info, url, r.text)
        return data

    # 获得分支结构信息
    def get_branch_info(self, session, data, credit_ticket, ent_id, href):
        pattern = '/gjjbj/gjjQueryCreditAction!fzjgFrame\.dhtml\?ent_id=.*?&regno=(.*?)&clear=true&urltag=.*?&credit_ticket=.*?'
        search_list = re.findall(pattern, href)
        if len(search_list) <= 0:
            return None

        url = 'http://{host}/gjjbj/gjjQueryCreditAction!fzjgFrame.dhtml?flag=more&ent_id={entid}&regno={regno}&credit_ticket={credit_ticket}'.format(
            host=self.host, entid=ent_id, credit_ticket=credit_ticket, regno=search_list[0])
        r = self.task_request(session.get, url)
        if r is None:
            return None
        self.append_model(data, Model.branch_info, url, r.text)
        return data

    def get_contributive_info(self, session, data, credit_ticket, ent_id):
        url = 'http://{host}/gjjbj/gjjQueryCreditAction!touzirenInfo.dhtml?chr_id=&credit_ticket={credit_ticket}'.format(
            host=self.host, credit_ticket=credit_ticket)
        post_data = {
            'pageNos': '1',
            'ent_id': ent_id,
            'urltag': '',
            'pageSize': '5',
            'pageNo': '1',
        }

        r = self.task_request(session.post, url, data=post_data)
        if r is None:
            return None

        self.append_model(data, Model.contributive_info, url, r.text, post_data=post_data)

        pattern = '共查询到&nbsp;.*?&nbsp;条信息&nbsp;&nbsp;共(.*?)页&nbsp;&nbsp;'
        search_list = re.findall(pattern, r.text.encode('utf-8'))
        if len(search_list) > 0:
            page_num = int(search_list[0])
            if page_num >= 50:
                page_num = 50
            for page in xrange(2, page_num + 1):
                post_data = {
                    'pageNos': page,
                    'ent_id': ent_id,
                    'urltag': '',
                    'pageSize': '5',
                    'pageNo': page - 1,
                }
                r = self.task_request(session.post, url, data=post_data)
                if r is None:
                    continue

                self.append_model(data, Model.contributive_info, url, r.text, post_data=post_data,
                                  classify=Model.type_detail)
        return data

    def get_other_info(self, session, data, text, credit_ticket, ent_id):
        if text == '':
            return

        jq = PyQuery(text, parser='html')
        frame_list = jq.find('iframe').items()

        for item in frame_list:
            href = item.attr('src')
            if href is None:
                continue

            # 出资信息
            if href.find('touzirenInfo') != -1:
                if self.get_contributive_info(session, data, credit_ticket, ent_id) is None:
                    self.get_detail_info(session, href, Model.contributive_info, data)

            # 主要人员信息
            if href.find('zyryFrame') != -1:
                if self.get_key_person_info(session, data, credit_ticket, ent_id) is None:
                    self.get_detail_info(session, href, Model.key_person_info, data)

            # 分支机构信息
            if href.find('fzjgFrame') != -1:
                if self.get_branch_info(session, data, credit_ticket, ent_id, href) is None:
                    self.get_detail_info(session, href, Model.branch_info, data)

            # 清算信息
            if href.find('qsxxFrame') != -1:
                self.get_detail_info(session, href, Model.liquidation_info, data)

            # 变更信息
            if href.find('biangengFrame') != -1:
                # 先查找是否有翻页的变更信息
                if self.get_change_info(session, data, credit_ticket, ent_id) is None:
                    self.get_detail_info(session, href, Model.change_info, data)

            # 股东信息
            if href.find('getTabForNB_new') != -1 and href.find('flag_num=1') != -1:
                self.get_detail_info(session, href, Model.shareholder_info, data)

            # 企业年报
            if href.find('qynbxxList') != -1:
                url = 'http://{host}{href}'.format(host=self.host, href=href)
                r = self.task_request(session.get, url)
                if r is None:
                    return

                for year, year_url in self.get_year_info_list(r.text):
                    r = self.task_request(session.get, year_url)
                    if r is None:
                        continue

                    self.append_model(data, Model.annual_info, url, r.text, year=year, classify=Model.type_detail)
                    self.get_nb_detail_info(session, r.text, data, year)

    def get_detail_html_list(self, seed, session, param_list):
        data_list = []
        rank = 0
        for item in param_list:
            try:
                credit_ticket = item.get('credit_ticket', None)
                ent_id = item.get('entId', None)

                # credit_ticket = 'CF6A23F1EA7CC12F9656974525B3DBAA'
                # ent_id = '6523000000001938'
                if credit_ticket is None or ent_id is None:
                    self.log.info('没有关键信息 item = {item}'.format(item=item))
                    continue

                # 基本信息
                url = 'http://{host}/gjjbj/gjjQueryCreditAction!openEntInfo.dhtml?entId={' \
                      'entId}&clear=true&urltag=1&credit_ticket={credit_ticket}'.format(host=self.host, entId=ent_id,
                                                                                        credit_ticket=credit_ticket)
                r = self.task_request(session.get, url)
                if r is None:
                    continue

                # 解析公司信息
                company_name = self.get_company_name(r.text)
                if company_name is None or company_name == '':
                    self.log.error('获取公司信息失败...{text}'.format(text=r.text))
                    continue

                # 建立数据模型
                rank += 1
                data = self.get_model(company_name, rank, seed, self.province)

                # 保存基础信息
                self.append_model(data, Model.base_info, url, r.text)

                # 获取其他信息
                self.get_other_info(session, data, r.text, credit_ticket, ent_id)

                data_list.append(data)
            except Exception as e:
                self.log.exception(e)

        self.target_db.insert_batch_data(self.target_table, data_list)
        return len(data_list)
